use axum_common::util::{md5, md5_salt};
use axum_common::util::{bcrypt, date, email};
use axum_common::{func_path, transform, ApiResult};
use chrono::{Local, NaiveDate};
use serde::{Deserialize, Serialize};

#[test]
fn test_md5() {
    let string1 = md5("123");
    let string2 = md5("123");
    assert_eq!(string1, string2);
}

#[test]
fn test_md5_salt() {
    let string1 = md5_salt("123", "abc");
    let string2 = md5_salt("123", "abc");
    println!("{string1}");
    println!("{string2}");
    assert_eq!(string1, string2);
}

#[test]
fn test_bcrypt() {
    let string1 = bcrypt::hash("123").unwrap();
    let string2 = bcrypt::verify("123", &string1);
    assert!(string2.is_ok());
    assert_eq!(string2.unwrap(), true);
}

#[test]
fn test_transform() {
    #[derive(Serialize)]
    struct Stu1 {
        name: String,
    }
    #[derive(Debug, PartialEq, Eq, Deserialize)]
    struct Stu2 {
        name: Option<String>,
    }
    let s1 = Stu1 {
        name: "sjt".to_string(),
    };
    let s2: ApiResult<Stu2> = transform!(&s1);

    assert!(s2.is_ok());
    assert_eq!(
        s2.unwrap(),
        Stu2 {
            name: Some("sjt".to_string())
        }
    );
}

#[tokio::test]
async fn test_email() {
    tracing_plus::init_tracing_log();
    let r = email::simple_email("476004058@qq.com", "test-email", "test-email-body")
        .await
        .unwrap();

    println!("{r:?}")
}

#[test]
fn test_func() {
    let a = test_func;
    let x = func_path!(a);

    println!("{x}")
}

#[test]
fn test_chrono() {
    // 2025-10-23 10:36:00
    let time = NaiveDate::from_ymd_opt(2025, 10, 23)
        .unwrap()
        .and_hms_opt(10, 36, 0)
        .unwrap()
        .and_local_timezone(Local)
        .unwrap();
    // 某天的开始
    let begin_of_day = date::begin_of_day(&time);
    assert_eq!(
        begin_of_day,
        NaiveDate::from_ymd_opt(2025, 10, 23)
            .unwrap()
            .and_hms_opt(0, 0, 0)
            .unwrap()
            .and_local_timezone(Local)
            .unwrap()
    );
    // 某天的结束
    let end_of_day = date::end_of_day(&time);
    assert_eq!(
        end_of_day,
        NaiveDate::from_ymd_opt(2025, 10, 23)
            .unwrap()
            .and_hms_opt(23, 59, 59)
            .unwrap()
            .and_local_timezone(Local)
            .unwrap()
    );

    // 某月的开始
    let begin_of_month = date::begin_of_month(&time);
    assert_eq!(
        begin_of_month,
        NaiveDate::from_ymd_opt(2025, 10, 1)
            .unwrap()
            .and_hms_opt(0, 0, 0)
            .unwrap()
            .and_local_timezone(Local)
            .unwrap()
    );

    // 某月的结束
    let end_of_month = date::end_of_month(&time);
    assert_eq!(
        end_of_month,
        NaiveDate::from_ymd_opt(2025, 10, 31)
            .unwrap()
            .and_hms_opt(23, 59, 59)
            .unwrap()
            .and_local_timezone(Local)
            .unwrap()
    );

    // 某季度的开始
    let begin_of_quarter = date::begin_of_quarter(&time);
    assert_eq!(
        begin_of_quarter,
        NaiveDate::from_ymd_opt(2025, 10, 1)
            .unwrap()
            .and_hms_opt(0, 0, 0)
            .unwrap()
            .and_local_timezone(Local)
            .unwrap()
    );

    // 某季度的结束
    let end_of_quarter = date::end_of_quarter(&time);
    assert_eq!(
        end_of_quarter,
        NaiveDate::from_ymd_opt(2025, 12, 31)
            .unwrap()
            .and_hms_opt(23, 59, 59)
            .unwrap()
            .and_local_timezone(Local)
            .unwrap()
    );

    // 某年的开始
    let begin_of_year = date::begin_of_year(&time);
    assert_eq!(
        begin_of_year,
        NaiveDate::from_ymd_opt(2025, 1, 1)
            .unwrap()
            .and_hms_opt(0, 0, 0)
            .unwrap()
            .and_local_timezone(Local)
            .unwrap()
    );

    // 某年的结束
    let end_of_year = date::end_of_year(&time);
    assert_eq!(
        end_of_year,
        NaiveDate::from_ymd_opt(2025, 12, 31)
            .unwrap()
            .and_hms_opt(23, 59, 59)
            .unwrap()
            .and_local_timezone(Local)
            .unwrap()
    );

    // 某周的开始
    let begin_of_week = date::begin_of_week(&time);
    assert_eq!(
        begin_of_week,
        NaiveDate::from_ymd_opt(2025, 10, 20)
            .unwrap()
            .and_hms_opt(0, 0, 0)
            .unwrap()
            .and_local_timezone(Local)
            .unwrap()
    );

    // 某周的结束
    let end_of_week = date::end_of_week(&time);
    assert_eq!(
        end_of_week,
        NaiveDate::from_ymd_opt(2025, 10, 26)
            .unwrap()
            .and_hms_opt(23, 59, 59)
            .unwrap()
            .and_local_timezone(Local)
            .unwrap()
    );
}
